<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <%@ include file="/WEB-INF/include/head.jsp" %>

</head>

<body class="sticky-header">

<section>
    <%@ include file="/WEB-INF/include/menu.jsp" %>


    <div class="main-content" style="min-height: 800px">
        <div class="header-section">

            <a class="toggle-btn"><i class="fa fa-bars"></i></a>

            <div class="menu-right">
                <ul class="notification-menu">
                    <li>
                        <a href="#" class="btn btn-default dropdown-toggle info-number" data-toggle="dropdown">
                            <i class="fa fa-envelope-o"></i>
                            <span class="badge">5</span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-head pull-right">
                            <h5 class="title">You have 5 Mails </h5>
                            <ul class="dropdown-list normal-list">
                                <li class="new">
                                    <a href="">
                                        <span class="thumb"><img src="${ctxStatic}images/photos/user1.png"
                                                                 alt=""/></span>
                                        <span class="desc">
                                          <span class="name">John Doe <span
                                                  class="badge badge-success">new</span></span>
                                          <span class="msg">Lorem ipsum dolor sit amet...</span>
                                        </span>
                                    </a>
                                </li>

                                <li class="new"><a href="">Read All Mails</a></li>
                            </ul>
                        </div>
                    </li>
                    <li>
                        <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                            <img src="${ctxStatic}images/photos/user-avatar.png" alt=""/>
                            John Doe
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-usermenu pull-right">

                            <li><a href="#"><i class="fa fa-cog"></i> 修改密码 </a></li>
                            <li><a href="#"><i class="fa fa-sign-out"></i> 退出</a></li>
                        </ul>
                    </li>

                </ul>
            </div>
            <!--notification menu end -->

        </div>
        <!-- header section end-->
        <div class="page-heading">
            <h3>
                修改公告
            </h3>
        </div>
        <!--body wrapper start-->
        <div class="wrapper">
            <div class="mail-box">
                <section class="mail-box-info">
                    <section class="mail-list">
                        <div class="compose-mail">
                            <form  method="post" id="signupForm">
                                <%--文件返回的id和notify的id隐藏域--%>
                                   <%-- <input type="hidden" id="notifyid" value="${requestScope.id}">
                                    <input type="hidden" id="types" value="${requestScope.type}">--%>
                                    <input type="hidden" id="attachmentIds" name="attachmentIds">

                                <div class="form-group">
                                    <label for="code" class="control-label col-md-1">公告编号</label>
                                    <div class="col-md-4 col-xs-11">
                                        <input id="code" name="code" type="text" value="${notify.code}" class="form-control" placeholder="请求公告编号">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="title" class="control-label col-md-1">标题</label>
                                    <div class="col-md-4 col-xs-11">
                                        <input id="title" value="${notify.title}" name="title" type="text" class="form-control"
                                               placeholder="请求标题">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="type" class="control-label col-md-1">添加类型</label>
                                    <div class="col-md-6 col-xs-11">
                                        <select class="form-control" style="width: 180px" id="type" name="type">
                                            <option  value="0">--请选择--</option>
                                            <option value="O" <c:if test='${notify.type== "O"}'>  selected='selected'</c:if>>外部公告</option>
                                            <option value="I" <c:if test='${notify.type== "I"}'>  selected='selected'  </c:if>>内部公告</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="control-label col-md-1">发至科室</label>
                                    <div class="col-md-9">
                                        <select multiple="multiple" class="multi-select" id="my_multi_select2"
                                                name="my_multi_select2[]">
                                            <%-- 此处显示从数据库获取到的内容--%>
                                            <c:choose>
                                                <c:when test="${notify.type=='O'}">
                                                    <c:forEach items="${farmList}" var="items">
                                                        <option>${items.parkName}</option>
                                                    </c:forEach>
                                                </c:when>
                                                <c:otherwise>
                                                    <c:forEach items="${userList}" var="list">
                                                        <option>${list.username}</option>
                                                    </c:forEach>
                                                </c:otherwise>
                                            </c:choose>
                                        </select>
                                    </div>
                                </div>
                                <div class="compose-editor">
                                    <textarea class="wysihtml5 form-control" rows="16" id="content">${notify.content}</textarea>
                                </div>
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件
                                    </button>
                                    <span id="demo2"></span>
                                    <button type="button" class="layui-btn" id="testListAction">开始上传</button>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4 col-xs-11">
                                        <button type="button" id="register" class="btn btn-primary">提交</button>
                                    </div>
                                    <div class="col-md-4 col-xs-11"></div>
                                </div>
                            </form>
                        </div>
                    </section>
                </section>
            </div>

        </div>

        <%@ include file="/WEB-INF/include/footer.jsp" %>

    </div>
</section>

<%@ include file="/WEB-INF/include/js.jsp" %>




<script type="html" id="template">
    {{#data}}
          <option value="{{id}}">{{resultName}}</option>
    {{/data}}

</script>
<script>
    $(function () {
        var template = $('#template').html();
        $('#my_multi_select2').multiSelect();
        $("#type").change(function () {
            var type = $(this).val();
            if(type!=0){
                utils.post({
                    url: "${ctx}/admin/notify/selectByType",
                    data: {
                        type: type
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            $.each(data.data, function (index, obj) {
                                $.extend(obj, {
                                    resultName: function () {
                                        if (this.parkName != null) {
                                            return obj.parkName;
                                        }
                                        if (this.username != null) {
                                            return obj.username;
                                        }
                                    }
                                });
                            })
                            var contentHtml = Mustache.render(template, data);
                            $('.multi-select').html(contentHtml);
                        }
                        $(".multi-select").multiSelect('refresh');
                    }
                })
            }
        });

            //验证部分
            $('#signupForm').validate({
                rules: {
                    //通知公告验证
                    code:{
                        required:true,
                        minlength:5
                    },
                    title:{
                        required:true,
                        rangelength:[5,30]
                    },
                    type:{
                        required:true,
                    },
                    content:{
                        required:true
                    }
                },
                messages: {
                    //通知公告
                    code:{
                        required:"请输入编号",
                        minlength:"编号不能小于5位数"
                    },
                    title:{
                        required:"请输入标题",
                        rangelength:"标题不能小于5或大于30位数"
                    },
                    type:{
                        required:"请选择要下发的类型"
                    },
                    content:{
                        required:"请输入内容"
                    }
                },
                errorPlacement: function (error, element) {
                    error.appendTo(element.parent());
                }
            });

        $("#register").click(function () {
                var flag = $("#signupForm").valid();
                if(!flag){
                    return;
                }
            layer.confirm("确认要提交吗？", {icon: 3,title: "提示信息！" }, function (index) {
                layer.close(index);
                utils.post({
                    url: '${ctx}/admin/notify/notifyUpdate',
                    data: {
                        id:$("#notifyid").val(),
                        type:$("#types").val(),
                        typechoose:$("#type").val(),
                        code:$("#code").val(),
                        title:$("#title").val(),
                        content:$("#content").val(),
                        attachmentIds:$("#attachmentIds").val(),
                        publishIds:$("#my_multi_select2").val()
                    },
                    success: function (data) {
                        if (data.code == 200) {
                            layer.alert('修改成功', {icon: 6,title:"提示信息！"}, function(index){
                                window.location.href = ctx + "/admin/notify/notifyIssued";
                            });
                        }
                    }
                });
            })
        })
    })

    //上传文件的方法
    var a=new Array();
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //选完文件后不自动上传
        upload.render({
            elem: '#testList'
            ,url: '${ctx}/common/upload'
            ,auto: false
            ,multiple: true
            ,bindAction: '#testListAction'
            ,choose:function (obj) {
                obj.preview(function(index, file, result){
                    $('#demo2').append(file.name+",");
                });
            }
            ,done: function(res){
                if (res.code == 200) {
                    a.push(res.data.id);
                    $("#attachmentIds").val(a);
                    layer.alert('上传成功', {icon: 6,title:"提示信息！"}, function(index){
                      layer.close(index);
                    });
                }
            },
            error: function (index, upload) {
                layer.alert('上传失败', {icon: 6,title:"提示信息！"}, function(index){
                    layer.close(index);
                });
            }
        });
    });

</script>
</body>
</html>
